	<?php $this->load->view('admin/partials/feed_menu'); ?>
	
	<?php 
		$type = isset($_GET['type']) ? $this->input->get('type') : 'Livescore';
		$key = isset($_GET['key']) ? $this->input->get('key') : '';
		
		$linkArray = defineFeedArrayData();
		$feedArray =  $linkArray[$type][$key];
		
		$matchArray = $this->match_entity->getAllMatches();
		$feedMatchId = array();
		foreach($matchArray as $item){
			if($item->feed_match_id != ''){
				$feedMatchId[] = $item->feed_match_id;
			}
		}
	?>
	
	<div class="clrboth"></div>
	<h3>Livescore &raquo; <?php echo $feedArray['title'];?></h3>
	<div class="clrboth"></div>
	
	<?php 
		$feedLink = $feedArray['link'];
		$string = file_get_contents($feedLink);
		$xml = new SimpleXMLElement($string);
		$res = $xml->xpath("//category"); 
		
		$baseInfoArray = $this->feed->getBaseInfo($type, $key);
		$baseInfoArray = array_reverse($baseInfoArray);
		
		$teamIDFeedDataArray = array();
		foreach($this->team_entity->getArrayListOfTeam() as $item){
			if($item->feed_team_id){
				$teamIDFeedDataArray[] = $item->feed_team_id;
			}
		}
	?>
	
	<table border="0" class="table-list">
		<thead>
			<tr>
				<th width="20%">Category</th>
				<th width="30%">Match</th>
				<th width="5%">Status</th>
				<th width="15%">Date/Time</th>
				<th width="20%">Information</th>
				<th width="10%">Action</th>
			</tr>
		</thead>
		
		<tbody>
			<?php $i=0; foreach($baseInfoArray as $k=> $val):?>
				<?php 
					$category_name = $val['category_name'];
					$category_id = $val['category_id'];
					$match_id = $val['match_id'];
					$match_date = $val['match_date'];
					$match_time = $val['match_time'];
					$match_status = $val['match_status'];
					$match_formatted_date = $val['match_formatted_date'];
					$localteam_name = $val['localteam_name'];
					$localteam_totalscore = $val['localteam_totalscore'];
					$localteam_type = $val['localteam_type'];
					$localteam_stat = $val['localteam_stat'];
					$localteam_id = $val['localteam_id'];
					$visitorteam_name = $val['visitorteam_name'];
					$visitorteam_totalscore = $val['visitorteam_totalscore'];
					$visitorteam_type = $val['visitorteam_type'];
					$visitorteam_stat = $val['visitorteam_stat'];
					$visitorteam_id = $val['visitorteam_id'];
					$comment = $val['comment'];
					$i++;
				?>
				<tr>
					<td><?php echo $category_name;?></td>
					<td>
						<?php echo $localteam_name."-".$localteam_totalscore." ".$localteam_stat." ($localteam_type)";?> 
						
						<?php if(!in_array($localteam_id,$teamIDFeedDataArray)):?>
							<a href="javascript:void(0);" onclick="return addTeamToDb(<?php echo $localteam_id;?>,'<?php echo addslashes($localteam_name);?>');">Add Team</a>
						<?php endif;?>
						<br/> Vs <br/>
						<?php echo $visitorteam_name."-".$visitorteam_totalscore." ".$visitorteam_stat." ($visitorteam_type)";?> 
						
						<?php if(!in_array($visitorteam_id,$teamIDFeedDataArray)):?>
							<a href="javascript:void(0);" onclick="return addTeamToDb(<?php echo $visitorteam_id;?>,'<?php echo addslashes($visitorteam_name);?>');">Add Team</a>
						<?php endif;?>
						<hr/>
						<?php echo $comment;?>
					</td>
					<td><?php echo $match_status;?></td>
					<td><?php echo $match_date." ".$match_time." - ".$match_formatted_date;?></td>
					<td>
						<a href="javascript:void(0);" onclick="return viewInning('<?php echo $type;?>','<?php echo $key;?>',<?php echo $category_id;?>,<?php echo $match_id;?>,<?php echo $i;?>);">Inning</a> <br/>
						<a href="javascript:void(0);" onclick="return viewComment('<?php echo $type;?>','<?php echo $key;?>',<?php echo $category_id;?>,<?php echo $match_id;?>,<?php echo $i;?>);">Commentaries</a> <br/>
						<a href="javascript:void(0);" onclick="return viewWicket('<?php echo $type;?>','<?php echo $key;?>',<?php echo $category_id;?>,<?php echo $match_id;?>,<?php echo $i;?>);">Wickets</a> <br/>
						<a href="javascript:void(0);" onclick="return viewLineup('<?php echo $type;?>','<?php echo $key;?>',<?php echo $category_id;?>,<?php echo $match_id;?>,<?php echo $i;?>);">Lineups</a> <br/>
						<a href="javascript:void(0);" onclick="return viewMatchInfo('<?php echo $type;?>','<?php echo $key;?>',<?php echo $category_id;?>,<?php echo $match_id;?>,<?php echo $i;?>);">Matchinfo</a> <br/>
						<?php echo loader_image("id=\"loader_image_$i\" class=\"hidden\"");?>
					</td>
					<td>
						<?php if($match_status != 'Finished' AND !in_array($match_id, $feedMatchId)): ?>
							<a href="javascript:void(0);" onclick="return addMatch('<?php echo $type;?>','<?php echo $key;?>',<?php echo $category_id;?>,<?php echo $match_id;?>,<?php echo $i;?>);">Add this match</a>
							<?php echo loader_image("id=\"loader_image_add_team_$i\" class=\"hidden\"");?>
						<?php endif;?>
						
						<?php if(in_array($match_id, $feedMatchId)):?>
							<?php $MATCH = $this->match_entity->init('feed_match_id', $match_id);?>
							<a href="<?php echo site_url("admin/dataman/match/in_tournament/")."?t_id={$MATCH->getTournamentId()}&fmid={$MATCH->getId()}";?>" >Go to this match in Tournament</a>
							
						<?php endif;?>
					</td>
				</tr>
			<?php endforeach;?>
		</tbody>
	</table>

<script type="text/javascript">
	jQuery(document).ready(function() {
		var options = { 
			beforeSubmit:function(formData, jqForm, options){ 
							return true;
						},   
			success:function(responseText, statusText, xhr, $form){
				if(responseText == 'ok'){
					reload();
				}else{
					alert(responseText);
				}
			}   
		};	
		var optionsMatch = { 
			beforeSubmit:function(formData, jqForm, options){ 
							return true;
						},  
			success:function(responseText, statusText, xhr, $form){
				if(responseText == 'ok'){
					reload();
				}else{
					alert(responseText);
				}
			}   
		};	
		jQuery("#add_team_submit").live("submit",function(e){
			e.preventDefault(); // <-- important
			jQuery(this).ajaxSubmit(options);
			return false;
		});
		jQuery("#match_submit").live("submit",function(e){
			e.preventDefault(); // <-- important
			jQuery(this).ajaxSubmit(optionsMatch);
			return false;
		});
	});
	
	function addTeamToDb(team_id, name){
		jQuery.post(BASE_URI+'admin/dataman/general/add_team',{},function(res){
			jQuery('#dialog').html(res);
			jQuery('#dialog').dialog(
				{
					 width: 650,
					 resizable: false,
					 height:200,
					 title: 'Add team' 
				}
			);
			jQuery('#dialog #name').attr('value',name);
			jQuery('#dialog #feed_team_id').attr('value',team_id);
		});
	}
	
	function addMatch(type, key, category_id, match_id,i){
		jQuery('#loader_image_add_team_'+i).show();
		jQuery.post(BASE_URI+'admin/dataman/feed/addMatch',{type:type,key:key,category_id:category_id,match_id:match_id},function(res){
			jQuery('#loader_image_add_team_'+i).hide();
			jQuery('#dialog').html(res);
			jQuery('#dialog').dialog(
				{
					 width: 650,
					 resizable: false,
					 height:500,
					 title: 'Add Match' 
				}
			);
		});
	}
	
	function viewInning(type, key, category_id, match_id,i){
		jQuery('#loader_image_'+i).show();
		jQuery.post(BASE_URI+'admin/dataman/feed/viewInning',{type:type,key:key,category_id:category_id,match_id:match_id},function(res){
			jQuery('#loader_image_'+i).hide();
			jQuery('#dialog').html(res);
			jQuery('#dialog').dialog(
				{
					 width: 850,
					 resizable: false,
					 height:700,
					 title: 'Inning Information' 
				}
			);
		});
	}
	
	function viewComment(type, key, category_id, match_id,i){
		jQuery('#loader_image_'+i).show();
		jQuery.post(BASE_URI+'admin/dataman/feed/viewComment',{type:type,key:key,category_id:category_id,match_id:match_id},function(res){
			jQuery('#loader_image_'+i).hide();
			jQuery('#dialog').html(res);
			jQuery('#dialog').dialog(
				{
					 width: 850,
					 resizable: false,
					 height:700,
					 title: 'Comment Information' 
				}
			);
		});
	}
	
	function viewWicket(type, key, category_id, match_id,i){
		jQuery('#loader_image_'+i).show();
		jQuery.post(BASE_URI+'admin/dataman/feed/viewWicket',{type:type,key:key,category_id:category_id,match_id:match_id},function(res){
			jQuery('#loader_image_'+i).hide();
			jQuery('#dialog').html(res);
			jQuery('#dialog').dialog(
				{
					 width: 850,
					 resizable: false,
					 height:700,
					 title: 'Wickets Information' 
				}
			);
		});
	}
	
	function viewLineup(type, key, category_id, match_id,i){
		jQuery('#loader_image_'+i).show();
		jQuery.post(BASE_URI+'admin/dataman/feed/viewLineup',{type:type,key:key,category_id:category_id,match_id:match_id},function(res){
			jQuery('#loader_image_'+i).hide();
			jQuery('#dialog').html(res);
			jQuery('#dialog').dialog(
				{
					 width: 850,
					 resizable: false,
					 height:700,
					 title: 'Lineups Information' 
				}
			);
		});
	}
	
	function viewMatchInfo(type, key, category_id, match_id,i){
		jQuery('#loader_image_'+i).show();
		jQuery.post(BASE_URI+'admin/dataman/feed/viewMatchInfo',{type:type,key:key,category_id:category_id,match_id:match_id},function(res){
			jQuery('#loader_image_'+i).hide();
			jQuery('#dialog').html(res);
			jQuery('#dialog').dialog(
				{
					 width: 850,
					 resizable: false,
					 height:700,
					 title: 'Match Information' 
				}
			);
		});
	}
</script>	

